
#include "Input.h"

#include "DirectInputDevice.h"

static BOOL CALLBACK DIEnumDeviceObjects(LPCDIDEVICEOBJECTINSTANCE lpddoi,LPVOID pvRef)
{

	CDirectInputDevice* pInput = (CDirectInputDevice*)pvRef;
	pInput->devObjInst.push_back( lpddoi );
	return DIENUM_CONTINUE;
}

CDirectInputDevice::CDirectInputDevice()
{
}

CDirectInputDevice::~CDirectInputDevice()
{
}

void CDirectInputDevice::Attach( CDirectInput* pDI,LPDIRECTINPUTDEVICE8 pDevice )
{
	m_pDevice.Attach ( pDevice );
	pDevice->EnumObjects( DIEnumDeviceObjects,this,DIDFT_ALL);
	m_pDirectInput = pDI;
}

void CDirectInputDevice::Release( )
{
	m_pDevice.Release();
}

bool CDirectInputDevice::Init( HWND hWnd )
{
	m_hWnd = hWnd;
	return true;
}

bool CDirectInputDevice::Update()
{
	return true;
}

void CDirectInputDevice::SetAxisMode( BOOL bAbsAxis )
{
	bool bHaveAbsAxis = HaveObject( DIDFT_ABSAXIS );

	DIPROPDWORD dipdw;
	HRESULT hr = S_OK;
	INT32 i = 0;

	dipdw.diph.dwSize       = sizeof(DIPROPDWORD); 
	dipdw.diph.dwHeaderSize = sizeof(DIPROPHEADER); 
	dipdw.diph.dwHow        = DIPH_DEVICE; 
	dipdw.diph.dwObj        = 0;


	if(bAbsAxis)
	{
		//if(HaveObject(iDev,DIDFT_ABSAXIS))
		{
			dipdw.dwData = DIPROPAXISMODE_ABS ;
			hr = m_pDevice->SetProperty(DIPROP_AXISMODE,(LPCDIPROPHEADER)&dipdw);
		}
	}
	else
	{
		//if( HaveObject(iDev,DIDFT_RELAXIS))
		{
			dipdw.dwData = DIPROPAXISMODE_REL ;
			hr = m_pDevice->SetProperty(DIPROP_AXISMODE,(LPCDIPROPHEADER)&dipdw);
		}
	}
}

void CDirectInputDevice::SetAbsAxisRange( int iAxis,int lMin,int lMax)
{
	
	DIPROPRANGE diprg; 
	HRESULT hr = S_OK;
	INT32 i = 0;

	diprg.diph.dwSize       = sizeof(DIPROPRANGE); 
	diprg.diph.dwHeaderSize = sizeof(DIPROPHEADER); 
	diprg.diph.dwHow        = DIPH_BYID; 
	diprg.lMin              = lMin; 
	diprg.lMax              = lMax; 

	if( HaveObject(DIDFT_ABSAXIS))
	{
		int nCount = (int)devObjInst.size();

		for(i = 0;i<nCount;i++)
		{	
			
			//diprg.diph.dwObj  = devObjInst[iCount ].dwType;	
			//hr = m_pDevice->SetProperty( DIPROP_RANGE, &diprg.diph ) ;
		}
	}
}

void CDirectInputDevice::SetAbsAxisDeadzone( int iAxis,int dwValue)
{
	/*
	DIPROPDWORD dipdw;
	HRESULT hr = S_OK;
	INT32 i = 0;

	dipdw.diph.dwSize       = sizeof(DIPROPDWORD); 
	dipdw.diph.dwHeaderSize = sizeof(DIPROPHEADER); 
	dipdw.diph.dwHow        = DIPH_BYID; 
	dipdw.dwData = dwValue;	

	if(__HaveObject(iDev,DIDFT_ABSAXIS))
	{
		for(i = 0;i<MAX_DEVOBJINST;i++)
		{	
			dipdw.diph.dwObj  = __DI_DevObjInst[iDev][i].dwType;	
			hr = __DI_Dev[iDev]->SetProperty( DIPROP_DEADZONE, &dipdw.diph ) ;
		}
	}*/
}

bool CDirectInputDevice::HaveObject( DWORD dwType )
{
	return GetObjectCount( dwType ) > 0 ? true:false;
}

int CDirectInputDevice::GetObjectCount( DWORD dwType )
{
	int nObjectCount = 0;

	int nCount = (int) devObjInst.size();
	
	for( int i = 0;i<nCount;i++)
	{
		if( dwType == DIDFT_GETTYPE( devObjInst[i]->dwType ))
		{
			nObjectCount++;
		}
	}
	return nObjectCount;
}